package extraidados;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


/**
 *
 * @author antonio
 */

public class Dados {
        //strings identificando o projeto de lei e a tag correpondente
	public char[] PL;
	public char[] TAG;
	public int NUM;
        //public void extraiPL(int Numero){

	public void Dados () {
		//char[] extra = { 'P','L'};
                //this.PL = extra;
                //char[] extra2 = { 'R','E','S','U','M','O' };
		//this.TAG = extra2;
                //this.NUM = 0;
                //System.out.println(this.NUM);
	}
        public void atualizaNUM (int novoNUM) {
            NUM = novoNUM;
            return;
        }
        public void atualizaTAG (char[] novaTAG) {
            TAG = novaTAG;
            return;
        }
        //o parâmetro PL é o Parâmetro inicial. Ele pode ser político por exemplo. O parâmetro TAG pode ser estado por exemplo. Logo essa
        //classe pode ser usada para ambos os casos de uso. NNum é a ocorrência no arquivo xml (loop).
        public String extrai (String PL, String TAG, int nnum, String Endereco) throws ParserConfigurationException, SAXException, IOException {
            String content = "";
            //System.out.println(this.PL);

            
            File fXmlFile = new File(Endereco);
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();
            //System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
            NodeList nList = doc.getElementsByTagName(PL);
            //System.out.println("-----------------------");
            //System.out.println(nList.getLength());
            //System.out.println(PL);
            //System.out.println(TAG);
            //System.out.println(nnum);
            if ( nnum < nList.getLength() ) {

                Node nNode = nList.item(nnum);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;

                    content = getTagValue(TAG, eElement);
                   

                }
            }
            
		/*for (int temp = 0; temp < nList.getLength(); temp++) {
 
		   Node nNode = nList.item(temp);
		   if (nNode.getNodeType() == Node.ELEMENT_NODE) {
 
		      Element eElement = (Element) nNode;
 
		      System.out.println("First Name : " + getTagValue("firstname", eElement));
		      System.out.println("Last Name : " + getTagValue("lastname", eElement));
	              System.out.println("Nick Name : " + getTagValue("nickname", eElement));
		      System.out.println("Salary : " + getTagValue("salary", eElement));
 
		   }
		}*/

            return content;
        }

        private static String getTagValue(String sTag, Element eElement) {
            NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

            Node nValue = (Node) nlList.item(0);

            return nValue.getNodeValue();
    }
 
}
